﻿using System.Web;
using Grishko.SerilizeTools;
using Grishko.DataTier.SqlSentence;
using OutdoorLib.BaseEntity.Face;
using OutdoorLib.BaseEntity.Infrastructure;
using OutdoorLib.BaseEntity.Constraction;
using OutdoorLib.BaseEntity.Dictionary;
using OutdoorLib.BaseEntity.WS;
using OutdoorLib.BaseEntity.Contract;
using System;

namespace OutdoorLib.BaseEntity.Filters
{
    /// <summary>
    /// Список сотрудников 
    /// </summary>
    [TagNameInfo(TagName = "ContractListFilter")]
    public class ContractListFilter: Filter,ITimePeriodFilter 
    {

        public class ContractListEngine : ScriptEngine, ITimePeriodFilter 
        {
            public ContractListEngine (ScriptEngine.TScriptEngineSrchInfo Settings) : base(Settings) { }
            public override string GetScript()
            {
                ///формируем ограничение по времени на всю выборку 
                ITimePeriodFilter TimePeriodFilter=(ITimePeriodFilter)(this);  
                string s_= Ecopsy .Tools.DateTimeTools .GetSqlDate(TimePeriodFilter.dtStart);
                string s__= Ecopsy .Tools.DateTimeTools .GetSqlDate(TimePeriodFilter.dtFinish);
                string[]  arr =  {s_,s__};                                                    
                string SqlFunction = string.Format ("GetContractObjectPropertyStorage({0},{1})", arr);
                
                ///формируем ограничение на обязательные условия - дата начала контракта в диапазоне Start Finish
                this.MandatoryConditions.AddMandatoryCondition_Between("StartDate", dtStart, dtFinish);           

                                                    
                this.EntityTypeInfo.SqlFunction = SqlFunction;
                return base.GetScript();
            }


            #region Члены ITimePeriodFilter

            private System.DateTime  _dtStart = Ecopsy .Tools .DateTimeTools .GetFisrtDayInMonth (DateTime .Now) ; 
            [EntityNonSerialized ]
            public System.DateTime dtStart
            {
                get { return _dtStart; }
                set { _dtStart = value; }
            }
            private System.DateTime _dtFinish = Ecopsy.Tools.DateTimeTools.GetLastDayInMonth (DateTime.Now);
            [EntityNonSerialized]
            public System.DateTime dtFinish
            {
                get { return _dtFinish; }
                set { _dtFinish = value; }
            }

            #endregion
        }

        public override string ObjectPageKey
        {
            get
            {
                return "PageKey_ContractEditor";
            }
        }


        public ContractListFilter()
        {
            ScriptEngine.TScriptEngineSrchInfo Settings = new ScriptEngine.TScriptEngineSrchInfo();
            Settings.EntityType = typeof(ContractEntity );            
            Where = new ContractListEngine(Settings);
        }

        #region Члены ITimePeriodFilter

        
        public System.DateTime dtStart
        {
            get { return (this.Where as ITimePeriodFilter).dtStart; }
            set { (this.Where as ITimePeriodFilter).dtStart = value; }            
        }

        public System.DateTime dtFinish
        {

            get { return (this.Where as ITimePeriodFilter).dtFinish; }
            set { (this.Where as ITimePeriodFilter).dtFinish = value; }            
        }

        #endregion
    }
}